//
//  QQFZ.m
//  10-10
//
//  Created by 钟纪旺 on 16/10/12.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "QQFZ.h"
#import "TVC.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
@interface QQFZ ()<UISearchResultsUpdating>
@property (nonatomic, strong)NSMutableArray *dataArray;//数据
@property (nonatomic, strong)NSMutableArray<NSNumber *> *isExpland;//这里用到泛型，防止存入非数字类型
@property(strong,nonatomic)UISearchController *searchCon;
@end

@implementation QQFZ
/*
 
 解决方法，在原来的基础之上再加一个section，作为第0个section，且section里面只有一行cell，此cell就为表识符为liu的cell，问题解决
 
command ＋f 输入“更改” 回车即可查看更改的地方
 
 
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    _searchCon = [[UISearchController alloc] initWithSearchResultsController:[self.storyboard instantiateViewControllerWithIdentifier:@"searchres"]];
    
    self.definesPresentationContext = YES;
    self.tableView.tableHeaderView = _searchCon.searchBar;
    self.title = @"联系人";
    [self loadData];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    //TVC *tvc = (UITableViewController *)searchController.searchResultsController;
}

- (void)loadData {
    if (!self.dataArray) {
        self.dataArray = [NSMutableArray array];
    }
    if (!self.isExpland) {
        self.isExpland = [NSMutableArray array];
    }
    
    self.dataArray = [NSArray arrayWithObjects:@[@"唐超",@"廖梦诗",@"小鸭子",@"王成龙"],@[@"李科宪",@"张振",@"no"],@[@"h",@"i",@"j",@"m",@"n"],nil].mutableCopy;
    //用0代表收起，非0代表展开，默认都是收起的
    for (int i = 0; i < self.dataArray.count; i++) {
        [self.isExpland addObject:@0];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     /* 更改      段数＋1*/
    return self.dataArray.count+1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     /* 更改      第一段只返回一行  */
    if (section == 0) {
        return 1;
    }else{
        section = section -1;
        NSArray *array = self.dataArray[section];
        if ([self.isExpland[section] boolValue]) {
            return array.count;
        }else{
            return 0;
        }

        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     /* 更改       第一段返回表识为liu的cell  */
    if (indexPath.section == 0 ) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"liu" forIndexPath:indexPath];
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lll" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"lll"];
    }
    
     /* 更改       此时要将这里的section－1否则数组会越界  */
    cell.textLabel.text = self.dataArray[indexPath.section-1][indexPath.row];
    
    return cell;
    
}


//自定义SectionHeader
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
     /* 更改       第一段的表头返回nil  */
    if (section == 0) {
        return nil;
    }
    
    UIButton *headerSection = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    headerSection.backgroundColor = [UIColor clearColor];
     /* 更改       此时要将这里的section－1否则数组会越界  */
    NSString *imgName = [self.isExpland[section-1] boolValue]?@"icon_drop_up":@"icon_drop_down";
    
    
    
    UIImage *img = [UIImage imageNamed:imgName];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = CGRectMake(kScreenWidth-img.size.width-20, (44-img.size.height)/2.0, img.size.width, img.size.height);
    [headerSection addSubview:imgView];
    
    //分割线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44-0.5, kScreenWidth, 0.5)];
    lineView.backgroundColor = [UIColor grayColor];
    [headerSection addSubview:lineView];
    
    headerSection.tag = 666+section;
    
    /* 更改       此时要将这里的section－1否则header的名字会变成1，2，3  */
    //标题
    [headerSection setTitle:[NSString stringWithFormat:@"好友分组第%@组",@(section-1)] forState:UIControlStateNormal];
    headerSection.titleLabel.font = [UIFont systemFontOfSize:16];
    [headerSection setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [headerSection addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    return headerSection;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    /*更改       将第一段的headerView隐藏*/
    if (section == 0) {
        return 0;
    }
    return 44;
}


- (void)buttonAction:(UIButton *)button {
    
    NSInteger section = button.tag - 666;
    
    /* 更改       此时要将这里的section－1否则数组会越界  */
    //纪录展开的状态
    self.isExpland[section-1] = [self.isExpland[section-1] isEqual:@0]?@1:@0;
    
    //刷新点击的section
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:section];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
